package com.erp.app.catalog.login.web;

import com.erp.framework.controller.action.SimpleSearchStyleAction;
import com.erp.framework.controller.action.SimpleSearchStyleActionTemplate;


import org.apache.struts.action.Action;
import org.apache.struts.util.MessageResources;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.HashMap;
import java.util.Iterator;

import com.erp.app.admin.sysParam.beans.SysParamList;
import com.erp.app.common.ProjectConstants;
import com.erp.app.util.web.WebUtil;
import com.erp.framework.SimpleConstants;
import com.erp.framework.controller.form.SimpleSearchStyleForm;
import com.erp.framework.model.criteria.SimpleBaseSrchCriteria;
import com.erp.framework.model.delegate.SimpleSearchMaintStyleDelegate;
import com.erp.framework.model.pk.SimpleBasePk;
import com.erp.framework.model.vo.SimpleBaseVo;
import com.erp.framework.util.BeanUtil;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.CopyUtil;
import com.erp.framework.util.logger.Logger;
import com.erp.framework.util.user.User;
import com.erp.app.common.select.beans.SelectDelegate;


public class CatalogHeaderAction extends SimpleSearchStyleActionTemplate{

	public static final String ACTION_SEARCH_EXTRA = "searchExtra";

	protected String getModuleName(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response){
		return null;
	}

    protected String getFunctionId(){
		return "catalogHeader.do";
	}
       
    protected String getMasterFormSessionName(){
		return "catalogHeaderForm";
	}
        
    protected Class getMasterFormClass(){
		return CatalogHeaderForm.class;
	}
    
    protected Class getMasterVOClass(){
		return null;
	}
    
    protected Class getSearchCriteriaClass(){
		return null;
	}

	public ActionForward processActionSearchExtra(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors) {
		System.out.println(" ****** processActionSearchExtra ****** ");

		CatalogHeaderForm form = (CatalogHeaderForm)formIn;
		HttpSession session = request.getSession(false);
		ActionForward actionForward = mapping.findForward("search");

		System.out.println(WebUtil.getAppropriateCompanyCode(session));
		System.out.println(form.getSrchDesc());
		System.out.println(form.getSrchMainCat());

		


		SelectDelegate sdg = new SelectDelegate();
		String table = "V_CATEGORY_ITEM_CLASS A";
        String[] columns = new String[5];
        columns[0] = "A.COMPANY_CODE";
        columns[1] = "A.ITEM_LINE";
        columns[2] = "A.ITEM_CATEGORY";
        columns[3] = "A.ITEM_CLASS";
        columns[4] = "A.ITEM_CLASS_DESC";

        String where = "A.COMPANY_CODE = '" + WebUtil.getAppropriateCompanyCode(session) + "' "
			    + (CommonUtil.isEmpty(form.getSrchMainCat())?" ":(" AND A.ITEM_LINE = '" + form.getSrchMainCat() + "' "))
				+ (CommonUtil.isEmpty(form.getSrchDesc())?" ":(" AND A.ITEM_CLASS_DESC = '" + form.getSrchDesc() + "' "))
				+ " AND A.STATUS = '" + SimpleConstants.GENERIC_ACTIVE + "' "
				+ " AND A.SHOW_IN_CATALOG = '" + SimpleConstants.GENERIC_YES + "' ";
        ArrayList rsltList = sdg.findFieldList(table, columns, where);
		
		HashMap nodelist = new HashMap();

		for (int i=0; i<rsltList.size(); i++){
			System.out.println(i);
			Hashtable hash = (Hashtable)rsltList.get(i);			
			if (!nodelist.containsKey((String)hash.get("ITEM_LINE"))){
				nodelist.put((String)hash.get("ITEM_LINE"),(String)hash.get("ITEM_LINE"));
			}
			if (!nodelist.containsKey((String)hash.get("ITEM_CATEGORY"))){
				nodelist.put((String)hash.get("ITEM_CATEGORY"),(String)hash.get("ITEM_CATEGORY"));
			}
			if (!nodelist.containsKey((String)hash.get("ITEM_CLASS"))){
				nodelist.put((String)hash.get("ITEM_CLASS"),(String)hash.get("ITEM_CLASS"));
			}
		}

		String nodes = new String();
		if (nodelist!=null && !nodelist.isEmpty()){
			Iterator itm = nodelist.keySet().iterator();
			while(itm.hasNext()){
				String nodeId = (String)nodelist.get((Object)itm.next());
				nodes = nodes+"'"+nodeId+"'";
				if (itm.hasNext()){
					nodes +=",";
				}			
			}
		}

		table = "T_CATEGORY_TREE a";
        String column = "a.ITEM_ID";

        where = (CommonUtil.isEmpty(nodes)?"1=1":("A.NODE_ID in (" + nodes + ") "));				
		ArrayList itemIdList = sdg.findFields(table, column, where);	

		String itemIds = new String();
		for (int i=0; i<itemIdList.size(); i++){
			String itemId = itemIdList.get(i).toString();
			if (itemIds.indexOf(itemId)==-1){
				if (i!=0){
					itemIds += ",";
				}
				itemIds = itemIds + "'" +itemIdList.get(i).toString()+ "'";			
			}
		}
		
		session.removeAttribute("ITEM_IDS");
		session.setAttribute("ITEM_IDS",itemIds);
		session.removeAttribute("PAGE_STATUS");
		session.setAttribute("PAGE_STATUS","ITEM_SEARCH");
		
		return actionForward;
	}
        
    protected void setOtherCriteria(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors, Object criteria){}
    
    protected SimpleBasePk getPk(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors){
		return null;
	}

    protected void setFormType(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors){}

	protected void initAction(ActionMapping mapping, ActionForm formIn,
        HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {
		System.out.println(" ****** initAction ****** ");
        super.initAction(mapping, formIn, request, response, errors);
        HttpSession session = request.getSession(false);   
		CatalogHeaderForm form = (CatalogHeaderForm) formIn;        
        ActionForward actionForward = new ActionForward(mapping.getInput());   
    }

	protected void initForm(ActionMapping mapping, ActionForm formIn,
        HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {
		System.out.println(" ****** initForm ****** ");
        super.initForm(mapping, formIn, request, response, errors);
        HttpSession session = request.getSession(false);
        CatalogHeaderForm form = (CatalogHeaderForm) formIn;
        ActionForward actionForward = new ActionForward(mapping.getInput());		
    }
}